<template>
    <h1 style="text-align: center;">注册页面</h1>

    <table>
        <tr>
            <td>用户名</td>
            <td>
                <input type="text" placeholder="请输入用户名" v-model="Info.UL_Name" >
            </td>
        </tr>

        <tr>
            <td>密码</td>
            <td>
                <input type="password" placeholder="请输入密码" v-model="Info.UL_Pwd" >
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="submit" value="注册" @click="SaveData"  >
            </td>
        </tr>

    </table>

</template>

<script setup lang="ts">

import { ref } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter();

const Info = ref({
    UL_Name:'',
    UL_Pwd:'',
})

const SaveData = () => {

    if(Info.value.UL_Name==''){
        alert('用户名不能为空!')
        return;
    }

    if(Info.value.UL_Pwd==''){
        alert('密码不能为空!')
        return;
    }

    //验证密码输入长度大于6位且小于14位
    if(Info.value.UL_Pwd.length<6 || Info.value.UL_Pwd.length>14){
        alert('密码长度必须大于6位且小于14位!')
        return;
    }
    axios({
        method:'post',
        url:'http://localhost:63517/api/User/UserRegion',
        data:{
            UL_Name:Info.value.UL_Name,
            UL_Pwd:Info.value.UL_Pwd,
        }
    })
    .then(res => {
        if(res.data>0){
            alert('注册成功');
            router.push('/Login')
        }
        else if(res.data==-1){
            alert('用户名已存在,请重新输入');
            return;
        }
        else{
            alert('注册失败');
            return;//
        }
    })
    .catch(err => {
        console.log(err)
    })
}


</script>
